package com.service.impl;

import com.controller.PaginationSupport;
import com.entity.Department;
import com.entity.Employee;
import com.entity.Salary;
import com.form.EmployeeInfo;
import com.form.EmployeeInfoForAdmin;
import com.form.Message;
import com.mapper.DepartmentMapper;
import com.mapper.EmployeeMapper;
import com.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    DepartmentMapper departmentMapper;
    public Message deleteEmployeeByUsername(String username) {
        if(employeeMapper.deleteEmployeeByUsername(username) == 1){
            return new Message(1,"Delete successfully!");
        }
        return  new Message(0,"Unknown error.");
    }

    public int count() {
        return employeeMapper.count();
    }

    @Override
    public Employee findByUserNameAndPwd(String username, String password) {
        return employeeMapper.findByUserNameAndPwd(username, password);
    }

    public int countOfOneSalart(int employeeId) {
        List<Salary> salarys = employeeMapper.checkSalarysByName(employeeId);
        return salarys.size();
    }

    @Override
    public PaginationSupport<Salary> checkSalaryByName(int employeeId, int pageNo, int pageSize) {
        int totalCount = countOfOneSalart(employeeId);
        int startIndex = PaginationSupport.convertFromPageToStartIndex(pageNo, pageSize);
        List<Salary> salarys = employeeMapper.checkSalarysOrderByName(employeeId, startIndex, pageSize);
        PaginationSupport<Salary> ps = new PaginationSupport<Salary>(salarys, totalCount, pageSize, startIndex);
        return ps;
    }

    @Override
    public EmployeeInfo findInfoByUsername(String username) {
        return employeeMapper.getEmployeeInfoByName(username);
    }

    @Override
    public Message updateEmployeeInfo(Employee employee) {
        int status = employeeMapper.updateEmployeeInfo(employee);
        if(status==1){
            return new Message(1,"Update successfully!");
        }
        return new Message(0,"Unknown error.");
    }

    @Override
    public PaginationSupport<EmployeeInfoForAdmin> checkAllemployees(int pageNo, int pageSize) {
        int totalCount = count();
        int startIndex = PaginationSupport.convertFromPageToStartIndex(pageNo, pageSize);
        List<EmployeeInfoForAdmin> employees = employeeMapper.checkAllEmployees(startIndex, pageSize);
        PaginationSupport<EmployeeInfoForAdmin> ps =
                new PaginationSupport<EmployeeInfoForAdmin>(employees, totalCount, pageSize, startIndex);
        return ps;
    }

    @Override
    public Message addEmployee(String username, int departmentId, int levelId) {
        EmployeeInfo employeeInfo=findInfoByUsername(username);
        if(employeeInfo!=null){
            return new Message(0,"Current username have exist,you should choose other username.");
        }
        Department department = departmentMapper.findDepartmentById(departmentId);
        if(department==null){
            return new Message(0,"This departmentId is not exist,you should choose a exist departmentId.");
        }
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 将LocalDate转换为Timestamp
        Timestamp currentTimestamp = Timestamp.valueOf(currentDate.atStartOfDay());
        Employee employee=new Employee();
        employee.setIsEmployee(0);
        employee.setEmployTime(currentTimestamp);
        employee.setUsername(username);
        employee.setDepartmentId(departmentId);
        employee.setLevelId(levelId);
        employee.setPassword("123456");//新创一个员工，登录密码默认为123456
        employee.setPhoto("tt.png");
        employee.setIsExpire(0);
        employeeMapper.addEmployee(employee);//创建员工
        return new Message(1,"Add successfully!");
    }

    /**
     * Find employees by keys pagination support.
     *
     * @param pageNo   the page no
     * @param pageSize the page size
     * @param keys     the keys
     * @return the pagination support
     */
    @Override
    public PaginationSupport<EmployeeInfoForAdmin> findEmployeesByKeys(int pageNo, int pageSize, String keys) {
        int totalCount = countOfKeys(keys);
        int startIndex = PaginationSupport.convertFromPageToStartIndex(pageNo,pageSize);
        List<EmployeeInfoForAdmin> employees = employeeMapper.findPageByKeys(keys,startIndex,pageSize);
        PaginationSupport<EmployeeInfoForAdmin> employeeList = new PaginationSupport<EmployeeInfoForAdmin>(employees,totalCount,
                pageSize,startIndex);
        return employeeList;
    }

    @Override
    public Message updateEmployeeInfoForAdmin(String username, int departmentId, int levelId) {
        Department department=departmentMapper.findDepartmentById(departmentId);
        if(department==null){
            return new Message(0,"The departmentId is ineffective.");
        }
        int status = employeeMapper.updateEmployeeForAdmin(username,departmentId,levelId);
        if(status==1){
            return new Message(1,"Update successfully!");
        }
        return new Message(0,"Unknown error.");
    }
    @Override
    public int countEmployeeByLevel(int level) {

        return employeeMapper.countOfEmployeeByLevel(level);
    }

    @Override
    public Message updatePhotoByUsername(String username, String url) {
        int status = employeeMapper.updatePhotoByUsername(username,url);
        if(status==1){
            return new Message(1,"Update photo successfully!");
        }
        return new Message(0,"Unknown error.");
    }

    private int countOfKeys(String keys) {
        return employeeMapper.getCountOfDepartByKeys(keys);
    }
}